const Path = require('path');
const { File, SyncFile } = require('ada-util');

class Locker {
    constructor(path) {
        this._path = Path.resolve(path, './.ada.lock');
        try {
            this._info = JSON.parse(new SyncFile(this._path).read());
        } catch (e) {
            this._info = {};
        }
    }

    get path() {
        return this._path
    }

    get(key) {
        return this._info[key];
    }

    set(key, value) {
        this._info[key] = value;
        return this;
    }

    save() {
        return new File(this._path).write(JSON.stringify(this._info));
    }
}

module.exports = Locker;